﻿using LbangCMS.EFModels;
using LbangCMS.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LbangCMS.Controllers
{
    public class NewsController : Controller
    {
        // GET: Content
        public ActionResult Index(int categoryId=0,int pageIndex=1,int pageSize=5)
        {
            int totalPage;
            var model = new NewsListModel();
            var categories = GetAllCategory();
            model.Categories = new List<NewCategoryModel>();
            if(categories!=null&&categories.Any())
            {
                foreach (var item in categories.OrderBy(o=>o.SortIndex).ToList())
                {
                    model.Categories.Add(new NewCategoryModel() { ID = item.ID, Name = item.Name, SortIndex = item.SortIndex });
                }
               
            }
            if (categoryId == 0 && categories.Any())
            {
                categoryId = categories.OrderBy(o => o.SortIndex).First().ID;
            }
            var news = GetNewsList(categoryId, pageIndex, pageSize, out totalPage);
            model.News = new List<NewsModel>();
            model.PageIndex = pageIndex;
            ViewBag.CategoryId = categoryId;
            var category = GetCategoryInfo(categoryId);
            ViewBag.CategoryName = category.Name;
            ViewBag.Title = category.Name;
            if (news!=null&&news.Any())
            {
                model.TotalPage = totalPage;
                foreach (var item in news)
                {
                    model.News.Add(new NewsModel() { ID=item.ID,Title=item.Title,Body=item.Body,CategoryId=item.CategoryId,CategoryName=category.Name,PublishTime=item.PublishTime,Source=item.Source,Summary=item.Summary,ConverSrc=item.CoverSrc });
                }
            }
            return View(model);
        }

        public ActionResult Detail(int id)
        {
            var model = new NewsModel();
            var news = GetNews(id);
            if(news!=null&&news.Status==1)//已发布的文章
            {
                var category = GetCategoryInfo(news.CategoryId);
                ViewBag.CategoryId = category.ID;
                model.ID = news.ID;
                model.Title = news.Title;
                model.Summary = news.Summary;
                model.Body = news.Body;
                model.CategoryId = news.CategoryId;
                model.CategoryName = category.Name;
                model.Source = news.Source;
                model.PublishTime = news.PublishTime;
                ViewBag.Title = news.Title;
                return View(model);

            }
            else
            {
                return RedirectToAction("NotFound", "Home");
            }
        }

        [ChildActionOnly]
        public ActionResult CategoryList(int categoryId)
        {
            var model = new NewCategoryListModel();
            model.CurrentCategoryId = categoryId;
            var categories = GetAllCategory();
            if(categories!=null&&categories.Any())
            {
                model.NewsCategories = new List<NewCategoryModel>();
                foreach (var item in categories.OrderBy(o=>o.SortIndex).ToList())
                {
                    model.NewsCategories.Add(new NewCategoryModel() { ID=item.ID,Name=item.Name,SortIndex=item.SortIndex });
                }
            }
            return PartialView("_CategoryListPartial", model);
        }

        #region private
        private List<ContentCategory> GetAllCategory()
        {
            using (var db = new Database())
            {
                return db.ContentCategories.OrderByDescending(o => o.SortIndex).ToList();
            }
        }

        private Content GetNews(int id)
        {
            using (var db = new Database())
            {
                return db.Contents.Where(o => o.ID == id && o.Status == 1).FirstOrDefault();
            }
        }

        private List<Content> GetNewsList(int categoryId,int pageIndex,int pageSize,out int totalPage)
        {
            using (var db = new Database())
            {
                if(categoryId>0)
                {
                    var count = db.Contents.Where(o => o.CategoryId == categoryId && o.Status == 1).Count();
                    totalPage = (int)Math.Ceiling(count / (pageSize * 1M));
                    return db.Contents.Where(o => o.CategoryId == categoryId && o.Status == 1).OrderByDescending(o => o.PublishTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    var count = db.Contents.Where(o => o.Status == 1).Count();
                    totalPage = (int)Math.Ceiling(count / (pageSize * 1M));
                    return db.Contents.Where(o =>o.Status == 1).OrderByDescending(o => o.PublishTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                
            }
        }

        private ContentCategory GetCategoryInfo(int categoryId)
        {
            using (var db = new Database())
            {
                return db.ContentCategories.Where(o => o.ID == categoryId).FirstOrDefault();
            }
        }
        #endregion
    }
}